package com.by.component.tencent.cos;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.*;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.File;

@Slf4j
@Configuration
public class TencentCosSupport {

    @Resource
    private COSClient client;

    @Resource
    private FileStorageService fileStorageService;

    public void putObject(String bucketName, String key, byte[] bytes) {
        fileStorageService.of(bytes)
                .setPlatform(bucketName)
                .setSaveFilename(key)
                .upload();
    }

    public void putObject(String bucketName, String key, File file) {
        client.putObject(bucketName, key, file);
    }

    public void putObject(String bucketName, String key, String content) {
        client.putObject(bucketName, key, content);
    }

    public void copyObject(String sourceBucketName, String sourceKey, String targetBucketName, String targetKey) {
        client.copyObject(sourceBucketName, sourceKey, targetBucketName, targetKey);
    }

    public String initiateMultipartUpload(String bucketName, String key) {
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, key);
        InitiateMultipartUploadResult result = client.initiateMultipartUpload(request);
        return result.getUploadId();
    }

    public String multipartUploadPart(String bucketName, String key,String uploadId, Integer partNumber,byte[] bytes) {
        UploadPartRequest uploadPartRequest = new UploadPartRequest();
        uploadPartRequest.setBucketName(bucketName);
        uploadPartRequest.setKey(key);
        uploadPartRequest.setUploadId(uploadId);
        uploadPartRequest.setPartSize(bytes.length);
        uploadPartRequest.setPartNumber(partNumber);
        uploadPartRequest.setInputStream(IoUtil.toStream(bytes));
        UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
        String eTag = uploadPartResult.getETag();
        return eTag;
    }

    public String multipartUploadPartComplete(String bucketName, String key, String uploadId, JSONArray jsonArray){
        com.qcloud.cos.model.CompleteMultipartUploadRequest request = new com.qcloud.cos.model.CompleteMultipartUploadRequest();
        request.setBucketName(bucketName);
        request.setKey(key);
        request.setUploadId(uploadId);
        request.setPartETags(jsonArray.toJavaList(PartETag.class));
        com.qcloud.cos.model.CompleteMultipartUploadResult completeMultipartUploadResult = client.completeMultipartUpload(request);
        log.info("[TencentCosSupport][multipartUploadPartComplete] completeMultipartUploadResult:[{}]", JSON.toJSONString(completeMultipartUploadResult));
        String eTag = completeMultipartUploadResult.getETag();
        return eTag;
    }

    public String uploadCommonFile(String bucketName, String key, byte[] bytes) {
        PutObjectResult putObjectResult = client.putObject(new PutObjectRequest(bucketName, key, IoUtil.toStream(bytes), null));
        return putObjectResult.getETag();
    }
}
